package com.phkj.simple;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @description 消息方
 * @param
 * @return
 * @author wlj
 * @date 2018/9/5 10:30
 */
public class Recv {
  //定义队列名
  private final static String QUEUE_NAME = "simple-example";

  public static void main(String[] argv) throws Exception {
    //创建连接
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    // 声明队列
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    // 定义队列的消费者
    Consumer consumer = new DefaultConsumer(channel) {

      // 获取消息
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties, byte[] body)
              throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println("前端系统接收信息 '" + message + "'");
      }
    };
    // 监听队列
    channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}